package com.asa.photopicker.photo;

import java.util.ArrayList;

import com.asa.photopicker.R;
import com.asa.photopicker.photo.Folder.Media;

//import com.heartsquare.consts.CommonConst;
//import com.yunio.core.utils.ViewUtils;
//import com.yunio.hsdoctor.R;
//import com.yunio.hsdoctor.entity.Folder;
//import com.yunio.hsdoctor.entity.Folder.Media;
//import com.yunio.hsdoctor.manager.NotificationViewController;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MessageSelectMediaFragment extends MediaBaseFragment {
	public final static int DEFAULT_MAX_IMAGE_COUNT = 9;
	public final static String EXTRA_MAX_COUNT = "max_count";
//	private final static int REQUEST_CODE_PREVIEW = 1;
	private TextView mTvCount, mTvPreview;
	private View mSendView;
	private int mCheckedCount;
	private ArrayList<Media> mSelectedMedias = new ArrayList<>();

	private int maxCount;

	public static MessageSelectMediaFragment newInstance(int maxCount) {
		MessageSelectMediaFragment fragment = new MessageSelectMediaFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(EXTRA_MAX_COUNT, maxCount);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		maxCount = getArguments().getInt(EXTRA_MAX_COUNT, DEFAULT_MAX_IMAGE_COUNT);
	}

	@Override
	protected void onInitView(View rootView) {
		super.onInitView(rootView);
		mTvCount = (TextView) rootView.findViewById(R.id.tv_count);
		mTvPreview = (TextView) rootView.findViewById(R.id.tv_preview);
		mSendView = rootView.findViewById(R.id.layout_send);
		mTvPreview.setOnClickListener(this);
		mSendView.setOnClickListener(this);
		onLoadData();
	}

	@Override
	public void onResume() {
		super.onResume();
		mCheckedCount = mSelectedMedias.size();
		setCheckedCount();
	}

	@Override
	protected void updateImageItem(View convertView, boolean isImage, final int position, final Media media) {
		final ViewHolder holder = (ViewHolder) convertView.getTag();
		Utils.setVisibility(holder.mCtvAvatar, View.VISIBLE);
		holder.mCtvAvatar.setChecked(media.isChecked());
		holder.mCtvAvatar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onImageCheckedClicked(holder, media);
			}
		});
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(getActivity(), "## " + position, Toast.LENGTH_SHORT).show();
				// TODO
				// getLocalFragmentManager().replaceFragmentForResult(REQUEST_CODE_PREVIEW,
				// MediaSelectScaleImageFragment.newInstance(position,
				// mFolder.getChildList(), mSelectedMedias, maxCount));
			}
		});
	}

	@Override
	protected void loadFolder(Folder folder) {
		for (Media media : mSelectedMedias) {
			if (folder.contains(media)) {
				folder.set(folder.indexOf(media), media);
			}
		}
		super.loadFolder(folder);
	}

	@Override
	protected boolean showCamera() {
		return false;
	}

	private void onImageCheckedClicked(ViewHolder holder, Media media) {
		if (!media.isChecked()) {
			if (mCheckedCount >= maxCount) {
				// NotificationViewController.showNotify(R.string.send_image_max_count);
				return;
			}
		}
		holder.mCtvAvatar.toggle();
		media.setChecked(holder.mCtvAvatar.isChecked());
		updateCheckedList(media);
		mCheckedCount = media.isChecked() ? mCheckedCount + 1 : mCheckedCount - 1;
		setCheckedCount();
	}

	private void updateCheckedList(Media media) {
		if (media.isChecked()) {
			mSelectedMedias.add(media);
		} else {
			if (mSelectedMedias.contains(media)) {
				mSelectedMedias.remove(media);
			}
		}
	}

	private void setCheckedCount() {
		mTvCount.setText(String.valueOf(mCheckedCount));
		mTvPreview.setClickable(mCheckedCount > 0);
		mSendView.setClickable(mCheckedCount > 0);
	}

	@Override
	protected int getLayoutResId() {
		return R.layout.fragment_message_select_image;
	}

	private void onSendClicked() {
		Intent intent = new Intent();
		intent.putParcelableArrayListExtra(Utils.KEY_MEDIAS, mSelectedMedias);
		getActivity().setResult(Activity.RESULT_OK, intent);
		getActivity().finish();
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.tv_preview:
			ArrayList<Media> medias = new ArrayList<>();
			medias.addAll(mSelectedMedias);
			Toast.makeText(getActivity(), "## preview", Toast.LENGTH_SHORT).show();
			// TODO
			// getLocalFragmentManager().replaceFragmentForResult(REQUEST_CODE_PREVIEW,
			// MediaSelectScaleImageFragment.newInstance(0, medias,
			// mSelectedMedias, maxCount));
			break;
		case R.id.layout_send:
			onSendClicked();
			break;
		default:
			break;
		}
	}

}
